package com.zany.system.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;



import com.zany.system.service.ParameterSingleService;
import com.zany.system.vo.ParameterSingleVO;
import com.zany.util.ZanyErrorMsg;
import com.zany.util.ZanyException;
import com.zany.util.BeansUtil.RequestBeanUtil;
import com.zany.util.action.AbstractActionSupport;
import com.zany.util.ajax.AjaxTools;
import com.zany.util.json.JSONException;
import com.zany.util.json.JsonTools;


public class ParameterSingleAction extends AbstractActionSupport{

	private static final long serialVersionUID = -1206681143606154127L;
	private static Log logger = LogFactory.getLog(ParameterSingleAction.class);
	private List<ParameterSingleVO> parameterList = new ArrayList<ParameterSingleVO>();
	private ParameterSingleVO parameterSingleVO = new ParameterSingleVO();
	private ParameterSingleService parameterSingleService;
	
	public void getList() throws ZanyException, JSONException{
		if(logger.isInfoEnabled()){logger.info("ParameterSingleAction.getList()");}
		response = ServletActionContext.getResponse();
		String flag = request.getParameter("flag");
		Integer pageTotal = parameterSingleService.getParameterSingleCount(parameterSingleVO);
		if(flag!=null){
			parameterSingleVO.setParamCode(request.getParameter("paramCode"));
			parameterSingleVO = parameterSingleService.getParameterSingleDetail(parameterSingleVO);
			parameterList.add(parameterSingleVO);
			AjaxTools.exAjax("{results:" + pageTotal + ",ZANY:" + JsonTools.lstBeanToJson(parameterList) + "}", response);
		}else{
			String start = request.getParameter("start");
			String pageStart = (start== null || start.equals("")?"1":start);
			parameterSingleVO.setPageMessage(Integer.valueOf(pageStart));
			parameterList = parameterSingleService.getParameterSingleList(parameterSingleVO);
			AjaxTools.exAjax("{results:" + pageTotal + ",ZANY:" + JsonTools.lstBeanToJson(parameterList) + "}", response);
		}
	}
	
	public void delete() throws ZanyException{
		if(logger.isInfoEnabled()){logger.info("ParameterSingleAction.delete()");}
		response = ServletActionContext.getResponse();
		String delId[] = request.getParameterValues("delId");
		try{
			if(delId.length>0){
				parameterSingleService.deleteParameterSingle(delId);
				AjaxTools.exAjax("{success:true}", response);
			}
		}catch (Exception e) {
			AjaxTools.exAjax("{success:false}", response);
			throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
		}
	}
	
	public void insert() throws Exception{
		if(logger.isInfoEnabled()){logger.info("ParameterSingleAction.insert()");}
		response = ServletActionContext.getResponse();
		parameterSingleVO = (ParameterSingleVO)RequestBeanUtil.getReqToBean(ParameterSingleVO.class,request);
		String flag = request.getParameter("flag");
		try{
			if(flag.equals("update")){
				parameterSingleService.updateParameterSingle(parameterSingleVO);
			}else{
				parameterSingleService.insertParameterSingle(parameterSingleVO);
			}
			AjaxTools.exAjax("{success:true}",response);
		}catch (Exception e) {
			AjaxTools.exAjax("{success:false}", response);
			throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
		}
	}
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		
	}

	public ParameterSingleService getParameterSingleService() {
		return parameterSingleService;
	}

	public void setParameterSingleService(
			ParameterSingleService parameterSingleService) {
		this.parameterSingleService = parameterSingleService;
	}
	
}
